(defun number-of-divisors (n)
  (loop for i from 1 to (floor (/ n 2)) count (= (mod n i) 0)))

;;; using the fact that 1 + 2 + ... + n = n * (n + 1) / 2
(print (do* ((i 1 (1+ i))
      (last 0 nod)
      (nod 1 (number-of-divisors i)))
     ((and (> (* nod last) 500)
	  (> (number-of-divisors (/ (* i (1- i)) 2)) 500))
     (/ (* i (1- i)) 2))))

(defun list-divisors (n)
  (loop for i from 1 to (sqrt n)
        when (= (mod n i) 0)
          collect i
          and unless (= i (/ n i))
                collect (/ n i)))


(defun triangle (n)
  (/ (* n (+ n 1)) 2))
(print 10)

(defun triangle-factors (limit)
  (loop for n from 1
        for tri = (triangle n)
        when (> (length (list-divisors tri)) limit)
          return tri))

(do  ((i 1 (1+ i))
     (sum 0 (+ i sum))) 
    ((> (number-of-divisors sum) 500) (print sum )))